﻿#include <wx/stdpaths.h>
#include "TuxSpySiteListDlg.h"
#include "TuxSpyNewSiteDlg.h"

wxString site_list_value[] = {
	wxT("SiteName"),
	wxT("SiteType"),
	wxT("SiteIP"),
	wxT("SitePort"),
	wxT("WlsPort"),
	wxT("WlsUser"),
	wxT("WlsVersion"),
	wxT("Database"),
//	wxT("Enable"),
	wxT("")
};

TuxSpySiteListDlg::TuxSpySiteListDlg( wxWindow* parent )
:
SiteListDlg( parent )
{
	//dbWrapper.DBExist();
	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString DBName = StdDataDir + wxT("\\FusionSpy.db"); 

	dbWrapper.DBOpen( DBName, ADMINTABLE);
	
	CurrSelected = -1;
}
TuxSpySiteListDlg::~TuxSpySiteListDlg()
{
	dbWrapper.DBClose();
}

void TuxSpySiteListDlg::OnListItemSelected( wxListEvent& event )
{
	//wxMessageBox(wxString::Format("%i",(int)(event.GetIndex())));  
	CurrSelected = (int)(event.GetIndex());

}
void TuxSpySiteListDlg::OnSiteConnect( wxCommandEvent& event )
{
	if ( CurrSelected == -1 ) 
		wxMessageBox(wxT("You Must choose one site!") );
	else {
		SetReturnCode( 1 );
		Destroy();
	}
}

Site TuxSpySiteListDlg::GetSelectedSite( )
{
	return sitearray.Item(CurrSelected);
}

void TuxSpySiteListDlg::OnSiteAdd( wxCommandEvent& event )
{
	TuxSpyNewSiteDlg* siteNew = new TuxSpyNewSiteDlg( this );
	if( siteNew->ShowModal() == IDOK ) {
		Site site = siteNew->GetSite();
		for ( int i = 0; i< (wxInt16)sitearray.GetCount(); i ++ ) {
			if( site.SiteName == sitearray.Item(i).SiteName ) {
				wxMessageBox(wxT("There is another site with the same Name!") );
				return;
			}
		}
		dbWrapper.SiteInsert( site );
		FillSiteList();
	}
}


void TuxSpySiteListDlg::OnSiteModify( wxCommandEvent& event )
{
	if ( CurrSelected == -1 ) 
		wxMessageBox(wxT("You haven't choose any one!") );
	else {
		TuxSpyNewSiteDlg* siteNew = new TuxSpyNewSiteDlg( this );
		siteNew->SetSite( sitearray.Item(CurrSelected) );
		if( siteNew->ShowModal() == IDOK ) {
			Site site = siteNew->GetSite();
			dbWrapper.SiteDelete( sitearray.Item(CurrSelected) ); //delete old from SiteList table.
			dbWrapper.SiteInsert( site );			//insert new Site into table.
			FillSiteList();
		}
	}

}

void TuxSpySiteListDlg::OnSiteDelete( wxCommandEvent& event )
{
	if ( CurrSelected == -1 ) 
		wxMessageBox(wxT("You haven't choose any one!") );
	else {
		if( wxMessageBox(wxT("Are you sure you want to DELETE this site?"),
			wxT("TuxSpy"), wxYES_NO, this) == wxYES ) {
			m_lstSiteList->DeleteItem(CurrSelected);
			dbWrapper.SiteDelete( sitearray.Item(CurrSelected) ); //Remove from SiteList Table.
//			dbWrapper.TuxAlarmDelete( sitearray.Item(CurrSelected).SiteName ); //Remove asso. Alarm From TuxAlarm Table
			sitearray.RemoveAt(CurrSelected);
		}
	}
}


void TuxSpySiteListDlg::OnInitDlg( wxInitDialogEvent& event )
{
	wxListItem itemCol;

	// Site List Init
	for ( int i = 0; !site_list_value[i].IsEmpty(); i ++ ) {
		itemCol.SetText(site_list_value[i]);
		m_lstSiteList->InsertColumn(i, itemCol);
	};

	FillSiteList();

	return;

}

int TuxSpySiteListDlg::FillSiteList( )
{
	wxInt16 nCount = 0;
	Site site;

	dbWrapper.SiteListQuery();

	sitearray = dbWrapper.getAllSite();
	nCount = (wxInt16)sitearray.GetCount();

	m_lstSiteList->Hide();	m_lstSiteList->DeleteAllItems();
	for ( int i = 0; i< nCount; i ++ ) {
		site = sitearray.Item(i);
		long tmp = m_lstSiteList->InsertItem( i, site.SiteName );
		m_lstSiteList->SetItemData( tmp, i );

		m_lstSiteList->SetItem(i, 1, site.SiteType);
		m_lstSiteList->SetItem(i, 2, site.SiteIP);
		m_lstSiteList->SetItem(i, 3, site.SitePort);
		m_lstSiteList->SetItem(i, 4, site.WlsAdminURL);
		m_lstSiteList->SetItem(i, 5, site.SiteAuth);
		m_lstSiteList->SetItem(i, 6, site.WlsVer);
		m_lstSiteList->SetItem(i, 7, site.DatabaseFile);
	}

	for( int i = 0; i< m_lstSiteList->GetColumnCount(); i ++ )
		m_lstSiteList->SetColumnWidth(i, wxLIST_AUTOSIZE_USEHEADER);

	m_lstSiteList->Show();

	return 1;
}